#!/usr/bin/env python
#-------------------------------------------------------------------------------
#   file KnobDemo.py
#   brief
#       A knob control demo
#
#   $Id: $
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#   Copyright and Disclaimer Notice
#
#   Copyright (c) 2014 A.C. Verbeck
#
#   This file is part of Mutable Instruments Suruthi Patch Display (MI SPD).
#
#   MI SPD is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   MI SPD is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with MI SPD.  If not, see <http://www.gnu.org/licenses/>.
#
#-------------------------------------------------------------------------------

__author__ = '$Author: ac.verbeck@gmail.com $'
__version__ = '$Rev: 271 $'
__date__ = '$Date: 2014-04-12 23:19:35 -0700 (Sat, 12 Apr 2014) $'
__license__ = 'MIT'

import os
import sys

import wx
import wx.lib.colourselect as csel

try:
    dirName = os.path.dirname(os.path.abspath(__file__))
except:
    dirName = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.append(os.path.split(dirName)[0])

try:
    from agw import knobctrl as KC
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.knobctrl as KC


#-------------------------------------------------------------------------------
#   KnobCtrlDemo class
#   This class is a GUI application that displays a number of different controls.
#-------------------------------------------------------------------------------
class KnobCtrlDemo(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.panel = wx.Panel(self, -1)        
        self.LayoutItems()

    def LayoutItems(self):
        leftbottomsizer_staticbox = wx.StaticBox(self.panel, -1, "Play With Me!")
        lefttopsizer_staticbox = wx.StaticBox(self.panel, -1, "Change My Properties!")

        self.knob1 = KC.KnobCtrl(self.panel, -1, size=(100, 100))
        self.knob2 = KC.KnobCtrl(self.panel, -1, size=(100, 100))

        self.knob1.SetTags(range(0, 151, 10))
        self.knob1.SetAngularRange(-45, 225)
        self.knob1.SetValue(45)

        self.knob2.SetTags(range(0, 151, 10))
        self.knob2.SetAngularRange(0, 270)
        self.knob2.SetValue(100)

        self.knobtracker1 = wx.StaticText(self.panel, -1, "Value = 45")
        self.knobtracker2 = wx.StaticText(self.panel, -1, "Value = 100")

        knobslider = wx.Slider(self.panel, -1, 4, 1, 10, style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
        tickslider = wx.Slider(self.panel, -1, 16, 3, 20, style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
        tagscolour = csel.ColourSelect(self.panel, -1, "Choose...", wx.BLACK)
        boundingcolour = csel.ColourSelect(self.panel, -1, "Choose...", wx.WHITE)
        firstcolour = csel.ColourSelect(self.panel, -1, "Choose...", wx.WHITE)
        secondcolour = csel.ColourSelect(self.panel, -1, "Choose...", wx.Colour(170, 170, 150))

        knobslider.SetValue(4)
        tickslider.SetValue(16)

        mainsizer = wx.BoxSizer(wx.VERTICAL)
        panelsizer = wx.BoxSizer(wx.HORIZONTAL)
        rightsizer = wx.FlexGridSizer(6, 2, 10, 10)
        leftsizer = wx.BoxSizer(wx.VERTICAL)
        leftbottomsizer = wx.StaticBoxSizer(leftbottomsizer_staticbox, wx.VERTICAL)
        lefttopsizer = wx.StaticBoxSizer(lefttopsizer_staticbox, wx.VERTICAL)
        
        lefttopsizer.Add(self.knob1, 1, wx.ALL|wx.EXPAND, 5)
        lefttopsizer.Add(self.knobtracker1, 0, wx.ALL, 5)
        leftsizer.Add(lefttopsizer, 1, wx.ALL|wx.EXPAND, 5)
        leftbottomsizer.Add(self.knob2, 1, wx.ALL|wx.EXPAND, 5)
        leftbottomsizer.Add(self.knobtracker2, 0, wx.ALL, 5)
        leftsizer.Add(leftbottomsizer, 1, wx.ALL|wx.EXPAND, 5)
        panelsizer.Add(leftsizer, 1, wx.EXPAND|wx.ALL, 20)
        
        label_1 = wx.StaticText(self.panel, -1, "Knob Radius: ")
        rightsizer.Add(label_1, 0, wx.ALIGN_CENTER_VERTICAL, 5)
        rightsizer.Add(knobslider, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
        label_2 = wx.StaticText(self.panel, -1, "Number Of Ticks: ")
        rightsizer.Add(label_2, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        rightsizer.Add(tickslider, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
        label_3 = wx.StaticText(self.panel, -1, "Tags Colour: ")
        rightsizer.Add(label_3, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        rightsizer.Add(tagscolour, 0)
        label_4 = wx.StaticText(self.panel, -1, "Bounding Color: ")
        rightsizer.Add(label_4, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        rightsizer.Add(boundingcolour, 0)
        label_5 = wx.StaticText(self.panel, -1, "First Gradient Color: ")
        rightsizer.Add(label_5, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        rightsizer.Add(firstcolour, 0)
        label_6 = wx.StaticText(self.panel, -1, "Second Gradient Color: ")
        rightsizer.Add(label_6, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        rightsizer.Add(secondcolour, 0)
        panelsizer.Add(rightsizer, 1, wx.ALL|wx.EXPAND, 20)

        self.panel.SetSizer(panelsizer)
        panelsizer.Layout()

        mainsizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(mainsizer)
        mainsizer.Layout()
        
        self.Bind(wx.EVT_COMMAND_SCROLL, self.OnKnobRadius, knobslider)
        self.Bind(wx.EVT_COMMAND_SCROLL, self.OnTicks, tickslider)
        self.Bind(KC.EVT_KC_ANGLE_CHANGED, self.OnAngleChanged1, self.knob1)
        self.Bind(KC.EVT_KC_ANGLE_CHANGED, self.OnAngleChanged2, self.knob2)
        tagscolour.Bind(csel.EVT_COLOURSELECT, self.OnTagsColour)
        boundingcolour.Bind(csel.EVT_COLOURSELECT, self.OnBoundingColour)
        firstcolour.Bind(csel.EVT_COLOURSELECT, self.OnFirstColour)
        secondcolour.Bind(csel.EVT_COLOURSELECT, self.OnSecondColour)

    def OnAngleChanged1(self, event):
        value = event.GetValue()
        self.knobtracker1.SetLabel("Value = " + str(value))
        wx.LogMessage("KnobCtrl 1:%s\n"%value)
        self.knobtracker1.Refresh()

    def OnAngleChanged2(self, event):
        value = event.GetValue()
        self.knobtracker2.SetLabel("Value = " + str(value))
        wx.LogMessage("KnobCtrl 2:%s\n"%value)
        self.knobtracker2.Refresh()

    def OnKnobRadius(self, event): 
        value = event.GetPosition()
        self.knob1.SetKnobRadius(value)
        event.Skip()

    def OnTicks(self, event): 
        minvalue = self.knob1.GetMinValue()
        maxvalue = self.knob1.GetMaxValue()
        therange = (maxvalue-minvalue)/(event.GetPosition()-1)
        tickrange = range(minvalue, maxvalue+1, therange)
        self.knob1.SetTags(tickrange)
        event.Skip()

    def OnTagsColour(self, event):
        self.knob1.SetTagsColour(event.GetValue())

    def OnBoundingColour(self, event):
        self.knob1.SetBoundingColour(event.GetValue())

    def OnFirstColour(self, event):
        self.knob1.SetFirstGradientColour(event.GetValue())        

    def OnSecondColour(self, event):
        self.knob1.SetSecondGradientColour(event.GetValue())        


#-------------------------------------------------------------------------------
#   Run Application
#   Generates main window
#-------------------------------------------------------------------------------
class RunApp(wx.App):
    def __init__(self, name):
        self.name = name
        wx.App.__init__(self, redirect=False)

    def OnInit(self):
        wx.Log.SetActiveTarget(wx.LogStderr())
        self.SetAssertMode(wx.PYAPP_ASSERT_DIALOG)
        frame = wx.Frame(None, -1, self.name, pos=(50,50), size=(200,100),
                        style=wx.DEFAULT_FRAME_STYLE, name=self.name)
        frame.Show(True)
        win = KnobCtrlDemo(frame)
        frame.SetSize((640, 480))
        win.SetFocus()

        self.SetTopWindow(frame)
        self.frame = frame
        return True
 

#-------------------------------------------------------------------------------
#   Main entry point
#   - Init the application
#   - Run the main loop
#-------------------------------------------------------------------------------
if __name__ == '__main__':
    app = RunApp('Knob Demo')
    app.MainLoop()

#
#   End KnobDemo.py
#
